/**
 * Created by GadflyBSD on 16/6/24.
 */
var k=999;
var islog=true;
var vt=10;
var gk=10;
function agk(){
	gk++;
	alert(gk)
}

function gurl(_str){
	window.location.href=_str
}

var gindex=1;
var arr=[1,1,1,1];
var u2 = 'http://wx.mywoka.com/App/angular.html';

function my_bangzhuzhongxinc($scope){
	$scope.$on('$ionicView.enter', function() {
		// alert(9999)
		$('.hhh').addClass('tabs-item-hide');
	})
	$scope.$on('$ionicView.leave', function() {
		// alert(9999)
		$('.hhh').removeClass('tabs-item-hide');
	})
}

var wait=60;
var baseUrl = 'http://manage.waka.cn/';
var angularUrl = baseUrl + 'index.php?s=/app/index/angular.html';
var templatePath = '/Public/template/Webapp/';
var meta = new Object();
var content = document.getElementsByTagName('meta');
for(var i = 0; i < content.length; i++){
	if(content[i].getAttribute('name') == 'Authcode')
		meta.appurl = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appurl')
		meta.verifyImg = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'upload')
		meta.upload = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'angular')
		meta.angular = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'appId')
		meta.appId = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'timestamp')
		meta.timestamp = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'nonceStr')
		meta.nonceStr = content[i].getAttribute('content');
	if(content[i].getAttribute('name') == 'signature')
		meta.signature = content[i].getAttribute('content');
}
wx.config({
	debug: false,
	appId: meta.appId,
	timestamp: meta.timestamp,
	nonceStr: meta.nonceStr,
	signature: meta.signature,
	jsApiList: ['scanQRCode', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage', 'openLocation', 'getLocation', 'closeWindow']
});
angular.module('start', ['ionic', 'starter.services', 'starter.controllers'])
	.run(function($ionicPlatform) {
		$ionicPlatform.ready(function() {
			if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
				cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
				cordova.plugins.Keyboard.disableScroll(true);
			}
			if (window.StatusBar) {
				StatusBar.styleDefault();
			}
		});
	})
	.config(function($stateProvider, $urlRouterProvider,$ionicConfigProvider) {
		$ionicConfigProvider.platform.ios.tabs.style('standard');
		$ionicConfigProvider.platform.ios.tabs.position('bottom');
		$ionicConfigProvider.platform.android.tabs.style('standard');
		$ionicConfigProvider.platform.android.tabs.position('bottom');
		$ionicConfigProvider.platform.ios.navBar.alignTitle('center');
		$ionicConfigProvider.platform.android.navBar.alignTitle('center');
		$ionicConfigProvider.platform.ios.views.transition('ios');
		$ionicConfigProvider.platform.android.views.transition('ios');
		$stateProvider
			.state('tab', {
				url: '/tab',
				abstract: true,
				templateUrl: templatePath + 'tabs.html',
				controller: 'tabCtrl'
			})
			.state('tab.register', {
				url: '/register',
				cache: false,
				views: {
					'tab-home': {
						templateUrl: templatePath + 'register.html',
						controller: 'register'
					}
				}
			})
			.state('tab.login', {
				url: '/login',
				cache: false,
				views: {
					'tab-home': {
						templateUrl: templatePath + 'login.html',
						controller: 'loginCtrl'
					}
				}
			})
			.state('tab.my', {
				url: '/my',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: templatePath + 'my.html',
						controller: 'my'
					}
				}
			})
			.state('tab.home', {
				url: '/home',
				views: {
					'tab-home': {
						templateUrl: templatePath + 'home.html',
						controller: 'homec'
					}
				}
			})
			.state('tab.userinfo', {
				url: '/userinfo',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: templatePath + 'my/userinfo.html',
					}
				}
			})
			.state('tab.realname', {
				url: '/realname',
				cache: false,
				views: {
					'tab-my': {
						templateUrl: templatePath + 'my/realname.html',
						controller: 'realname'
					}
				}
			})
			.state('tab.tishenedu', {
				url: '/tishenedu',
				views: {
					'tab-account': {
						templateUrl: 'template/account_tishenedu.html',
					}
				}
			})
			.state('tab.tishenedu_home', {
				url: '/tishenedu_home',
				views: {
					'tab-home': {
						templateUrl: 'template/account_tishenedu.html',
					}
				}
			})
			.state('tab.youhuiquan', {
				url: '/youhuiquan',
				views: {
					'tab-account': {
						templateUrl: 'template/account_youhuiquan.html',
					}
				}
			})
			.state('tab.yinhangka', {
				url: '/yinhangka',
				views: {
					'tab-account': {
						templateUrl: 'template/account_yinhangka.html',
					}
				}
			})
			.state('tab.haikuangmingxi', {
				url: '/haikuangmingxi',
				views: {
					'tab-home': {
						templateUrl: 'template/home_haikuangmixing.html',
					}
				}
			})
			.state('tab.haikuangmingxi2', {
				url: '/haikuangmingxi2',
				views: {
					'tab-account': {
						templateUrl: 'template/home_haikuangmixing.html',
					}
				}
			})
			.state('tab.shops', {
				url: '/shops',
				views: {
					'tab-home': {
						templateUrl: 'template/shops.html',
					}
				}
			})
			.state('tab.shop', {
				url: '/shop',
				views: {
					'tab-home': {
						templateUrl: 'template/shop.html',
					}
				}
			})
			.state('tab.haikuang_account', {
				url: "/haikuang_account",
				views: {
					'tab-account': {
						templateUrl: "template/haikuang.html",
						controller:'haikuangcc'
					}
				}
			})
			.state('tab.haikuang', {
				url: "/haikuang",
				views: {
					'tab-home': {
						templateUrl: "template/haikuang.html",
						controller:'haikuangcc'
					}
				}
			})
			.state('tab.home_jiekuang', {
				url: "/home_jiekuang",
				views: {
					'tab-home': {
						templateUrl: "template/home_jiekuang.html",
						controller:'home_jiekuangc'
					}
				}
			})
			.state('tab.home_jilu', {
				url: "/home_jilu",
				views: {
					'tab-home': {
						templateUrl: "template/home_haikuanjilu.html",
						controller:'home_haikuanjilucc'
					}
				}
			})
			.state('tab.account_jilu', {
				url: "/account_jilu",
				views: {
					'tab-account': {
						templateUrl: "template/home_haikuanjilu.html",
						controller:'home_haikuanjilucc'
					}
				}
			})
			.state('tab.account', {
				url: '/account',
				views: {
					'tab-account': {
						templateUrl: 'template/account.html',
						controller: 'accountc'
					}
				}
			})
			.state('tab.discovery', {
				url: '/discovery',
				views: {
					'tab-discovery': {
						templateUrl: 'template/discovery.html',
						controller: 'discoveryc'
					}
				}
			})
			.state('tab.discovery_yaoqingjilu_4_14', {
				url: '/discovery_yaoqingjilu_4_14',
				views: {
					'tab-discovery': {
						templateUrl: 'template/discovery_yaoqingjilu_4_14.html',
					}
				}
			})
			.state('tab.discovery_fujinderen_4_13', {
				url: '/discovery_fujinderen_4_13',
				views: {
					'tab-discovery': {
						templateUrl: 'template/discovery_fujinderen_4_13.html',
					}
				}
			})
			.state('tab.discovery_yaoqingyoujiang_4_13', {
				url: '/discovery_yaoqingyoujiang_4_13',
				views: {
					'tab-discovery': {
						templateUrl: 'template/discovery_yaoqingyoujiang_4_13.html',
					}
				}
			})
			.state('tab.discovery_heimingdang_4_13', {
				url: '/discovery_heimingdang_4_13',
				views: {
					'tab-discovery': {
						templateUrl: 'template/discovery_heimingdang_4_13.html',
					}
				}
			})
			.state('tab.bangzhuzhongxin', {
				url: '/bangzhuzhongxin',
				views: {
					'tab-my': {
						templateUrl: 'template/my_bangzhuzhongxin.html',
						controller: 'my_bangzhuzhongxinc'
					}
				}
			})
			.state('tab.anquanbaozhang', {
				url: '/anquanbaozhang',
				views: {
					'tab-my': {
						templateUrl: 'template/my_anquanbaozhang.html',
					}
				}
			})
			.state('tab.guanyuwomen', {
				url: '/guanyuwomen',
				views: {
					'tab-my': {
						templateUrl: 'template/my_guanyuwomen.html',
					}
				}
			})
			.state('tab.gerenxiaoxi', {
				url: '/gerenxiaoxi',
				views: {
					'tab-my': {
						templateUrl: 'template/my_gerenxiuxi.html',
					}
				}
			})
			.state('tab.gerenshezhi', {
				url: '/gerenshezhi',
				views: {
					'tab-my': {
						templateUrl: 'template/my_gerenshezhi.html',
					}
				}
			})
			.state('tab.my_chushengriqishezhi_4_13', {
				url: '/my_chushengriqishezhi_4_13',
				views: {
					'tab-my': {
						templateUrl: 'template/my_chushengriqishezhi_4_13.html',
					}
				}
			})
			.state('tab.my_nichengshezhi_4_13', {
				url: '/my_nichengshezhi_4_13',
				views: {
					'tab-my': {
						templateUrl: 'template/my_nichengshezhi_4_13.html',
					}
				}
			})
			.state('tab.my_xingbieshezhi_4_13', {
				url: '/my_xingbieshezhi_4_13',
				views: {
					'tab-my': {
						templateUrl: 'template/my_xingbieshezhi_4_13.html',
						// controller: 'myc'
					}
				}
			})
			.state('tab.my_zhedieliebiao_4_13', {
				url: '/my_zhedieliebiao_4_13',
				views: {
					'tab-my': {
						templateUrl: 'template/my_zhedieliebiao_4_13.html',
						// controller: 'myc'
					}
				}
			})
			.state('tab.xitongshezhi', {
				url: '/xitongshezhi',
				views: {
					'tab-my': {
						templateUrl: 'template/my_xitongshezhi.html',
						// controller: 'myc'
					}
				}
			});
		$urlRouterProvider.otherwise('/tab/home');
	});

angular.module('starter.services', [])
	.factory('myService', function($http){
		var http = function(param){
			return $http({
				url: angularUrl,
				method: "POST",
				headers: {'Content-Type': 'application/x-www-form-urlencoded'},
				data: angular.extend({ac: 'angular'}, param)
			});
		}
		function xhr(param){
			return $http({
				url: angularUrl,
				method: "POST",
				headers: {'Content-Type': 'application/x-www-form-urlencoded'},
				data: param
			});
		}
		return {
			test: function(){
				"获得用户SESSION数据";
				return http({op: 'test'});
			},
			getBaseData: function(){
				return xhr({ac: 'getBaseData'});
			},
			register:function(data){
				"注册用户";
				return http(angular.extend({op: 'register'}, data));
			},
			login: function(data){
				"用户登录";
				return http(angular.extend({op: 'login'}, data));
			},
			getSession: function(refresh){
				"获得用户SESSION数据";
				return http({op: 'getSession', refresh: refresh});
			},
			sendSms: function(mobile, action){
				"发送短信验证码";
				return http({op: 'sendSms', mobile: mobile, action: action}, 'post');
			},
			getSurvey: function(param){
				"获取指定学校的竟调员";
				return http({op: 'getSurvey', schoolname: param}, 'get');
			},
			setRealname: function(data){
				"实名认证申请操作";
				return http(angular.extend({op: 'setRealname'}, data), 'post');
			},
			getUserInfo: function(){
				"获得用户详细数据";
				return http({op: 'getUserInfo'}, 'post');
			}
		};
	})
	.factory('myPopup', function($ionicPopup){
		"弹出框服务";
		return {
			alert: function(param){
				"alert（警告） 对话框";
				switch(param.type){
					case 'success':
						var title = (typeof param.title == 'null')?'操作成功啦!':param.title;
						var icon = 'ion-checkmark-round';
						break;
					case 'error':
						var title = (typeof param.title == 'null')?'操作失败啦!':param.title;
						var icon = 'ion-close-round';
						break;
					default:
						var title = (typeof param.title == 'null')?'系统操作提示!':param.title;
						var icon = 'ion-information-circled';
						break;
				}
				return $ionicPopup.alert({
					title: title,
					template: '<div class="row"><div class="col col-25"><i class="icon '+icon+'"></i></div><div class="col col-75">'+param.content+'</div></div>'
				});
			}
		}
	})
	.factory('poplog', function($ionicPopup){
		"弹出框服务";
		return {
			alert: function(param){
				"alert（警告） 对话框";
				switch(param.type){
					case 'success':
						var title = (typeof param.title == 'null')?'操作成功啦!':param.title;
						var icon = 'ion-checkmark-round';
						break;
					case 'error':
						var title = (typeof param.title == 'null')?'操作失败啦!':param.title;
						var icon = 'ion-close-round';
						break;
					default:
						var title = (typeof param.title == 'null')?'系统操作提示!':param.title;
						var icon = 'ion-information-circled';
						break;
				}
				return $ionicPopup.alert({
					// title: 'title',
					// content:'aaaa',
					templateUrl:'template/login.html',
					// template: '<div class="row"><div class="col col-25"><i class="icon '+icon+'"></i></div><div class="col col-75">'+param.content+'</div></div>'
				});
			}
		}
	})
	.service('ss',function(){
		this.s1=function(){
			// alert(99)
		}
	})
	.directive('pwCheck', function(){
		return {
			require: "ngModel",
			link: function(scope, elem, attrs, ctrl){
				var otherInput = elem.inheritedData("$formController")[attrs.pwCheck];
				ctrl.$parsers.push(function(value){
					if(value === otherInput.$viewValue){
						ctrl.$setValidity("repeat", true);
						return value;
					}
					ctrl.$setValidity("repeat", false);
				});
				otherInput.$parsers.push(function(value){
					ctrl.$setValidity("repeat", value === ctrl.$viewValue);
					return value;
				});
			}
		}
	})
	.directive('uploadWidget', function(){
		"上传指令";
		return {
			restrict: 'A',
			template: '<ul class="weui_uploader_files"></ul><div class="weui_uploader_input_wrp"><input class="weui_uploader_input" type="button" /></div>',
			link: function(scope, elements, attrs){
				elements.bind('click', function(){
					wx.ready(function(){
						var count = (attrs.uploadWidget == 'survey_other')?9:1;
						wx.chooseImage({
							count: count, // 默认9
							sizeType: ['original', 'compressed'],   // 可以指定是原图还是压缩图，默认二者都有
							sourceType: ['album', 'camera'],        // 可以指定来源是相册还是相机，默认二者都有
							success: function (res) {
								var localIds = res.localIds;        // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片
								elements.find('ul').prepend('<li class="weui_uploader_file"><img src="' + localIds + '" /></li>');
								wx.uploadImage({
									localId: localIds.toString(),
									isShowProgressTips: 1,
									success: function (resu) {
										var serverId = resu.serverId;
										elements.find('div').addClass('hidden');
										switch(attrs.uploadWidget){
											case 'idcard1':
												scope.realname.idcard1 = serverId;
												break;
											case 'idcard2':
												scope.realname.idcard2 = serverId;
												break;
											case 'xscard':
												scope.realname.xscard = serverId;
												break;
										}
									}
								});
							}
						});
					});
				});
			}
		}
	})
	.directive('buttonWidget', function(myService, $ionicLoading, myPopup){
		"按钮指令";
		return {
			restrict: 'A',
			scope: {
				forms: '='
			},
			link: function(scope, element, attrs){
				switch(attrs.buttonWidget){
					case 'sms':
						"发送短信验证码按钮";
					function sendSmsTimer(){
						"use strict";
						if (wait == 0) {
							element.removeAttr('disabled').text("重新发送验证码");
							wait = 60;
						} else {
							element.attr('disabled', 'disabled').text("(" + wait + "秒后)重发");
							wait--;
							setTimeout(function(){sendSmsTimer()}, 1000)
						}
					}
						element.bind('click', function(){
							$ionicLoading.show({content: 'Loading', animation: 'fade-in', showBackdrop: true, maxWidth: 200, showDelay: 0});
							myService.sendSms(attrs.mobile, attrs.ac).success(function(resp){
								$ionicLoading.hide();
								if(resp.errcode == 0){
									scope.forms.sms_order_id = resp.data.order_id;
									scope.forms.sms_order_date = resp.data.order_date;
								}
								myPopup.alert(resp);
							});
							sendSmsTimer();
						});
						break;
					case 'removePreson':
						if(attrs.forRole == 'boss'){
							element.attr('disabled', 'disabled');
						}else{
							element.bind('click', function(){
								myService.removePreson(attrs.forUid).success(function(resp){
									myPopup.alert(resp);
								});
							});
						}
						break;
				}
			}
		}
	});